import static org.junit.Assert.*;

import java.lang.reflect.Field;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 
 */

/**
 * COSC 617 Project 4 Task 4
 * 
 * @author Hui Liu
 *
 */
public class VendingMachineItemTest {
	public VendingMachineItem item;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		 
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {

	}

	/**
	 * Test method for {@link VendingMachineItem#VendingMachineItem(java.lang.String, double)}.
	 * test create a new VendingMachineItem correctly
	 */
	@Test
	public final void testVendingMachineItem() throws Exception {
		// test create a new VendingMachineItem correctly
		item= new VendingMachineItem("Diet Pepsi", 1.5);
		assertNotNull("VendingMachineItem is not created", item);
		// check type of the created object
		assertEquals(VendingMachineItem.class, item.getClass());
		
		//test if name is correctly assigned
		//find the value of private fields name
		Field nameField = item.getClass().getDeclaredField("name");
		nameField.setAccessible(true);
		assertEquals("Diet Pepsi", nameField.get(item));
		
		//test if price is correctly assigned
		//find the value of private fields price
		Field priceField = item.getClass().getDeclaredField("price");
		priceField.setAccessible(true);
		assertEquals(1.5, priceField.get(item));
	}
	
	/**
	 * Test method for {@link VendingMachineItem#VendingMachineItem(java.lang.String, double)}.
	 * if price is less than zero, create a new VendingMachineItem should throw VendingMachineException
	 */
	@Test(expected=VendingMachineException.class)
	public final void testVendingMachineItemInvalidPrice() {
		// test if price is less than zero
		item= new VendingMachineItem("Diet Pepsi", -1.5);
	}	
	

	/**
	 * Test method for {@link VendingMachineItem#getName()}.
	 */
	@Test
	public final void testGetName() {
		// test get the name correctly
		item= new VendingMachineItem("Diet Pepsi", 1.5);
		assertEquals("Diet Pepsi", item.getName());
	}

	/**
	 * Test when name of the item is null
	 */
	@Test(expected=VendingMachineException.class)
	public final void testNullName() {
		// test if price is less than zero
		item= new VendingMachineItem(null, 1.5);
	}	

	/**
	 * Test method for {@link VendingMachineItem#getPrice()}.
	 */
	@Test
	public final void testGetPrice() {
		// test get the price correctly
		item= new VendingMachineItem("Diet Pepsi", 1.5);
		assertEquals(1.5, item.getPrice(), 0.0001);
	}

}
